package ioperation;

import book.Book;
import book.BookList;

import java.util.Arrays;
import java.util.Scanner;

public class AddOperation implements IOperations{

    @Override
    public void work(BookList bookList) {
        System.out.println("--------增加图书--------");
        Scanner s = new Scanner(System.in);

        System.out.println("请输入书名：");
        String name = s.nextLine();

        System.out.println("请输入作者：");
        String author = s.nextLine();

        System.out.println("请输入类型：");
        String type = s.nextLine();

        System.out.println("请输入价格：");
        int price = s.nextInt();


        int currentSize = bookList.getUsedSize();
        //先要看该书架上是否有你已经添加过的书
        for(int i = 0; i < currentSize; i++) {
            if(bookList.getBook(i).getName().equals(name)) {
                System.out.println("书架上已经有你想要添加的书了!!!");
                return;
            }
        }

        //直接在数组最后添加


        //然后再进行判断是否书架（数组）满了
        if(currentSize == bookList.getBooks().length) {
//            //数组已满
//            System.out.println("书架已满不能再放书!!!");
//            return;
            //扩容
            bookList.setBooks(Arrays.copyOf(bookList.getBooks(), currentSize * 2));
        }

        Book book = new Book(name,author,price,type);
        bookList.setBook(book,currentSize);
        bookList.setUsedSize(currentSize + 1);
        System.out.println("添加成功!!!");



    }
}
